package com.java2007.zhangyu.restaurant.controller.backend;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.java2007.zhangyu.restaurant.constant.ServletConstant;
import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.entity.Result;
import com.java2007.zhangyu.restaurant.pojo.DinnerTable;
import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.AdminService;
import com.java2007.zhangyu.restaurant.service.TableService;
import com.java2007.zhangyu.restaurant.service.impl.AdminServiceImpl;
import com.java2007.zhangyu.restaurant.service.impl.TableServiceImpl;
import com.java2007.zhangyu.restaurant.utils.WebUtil;
import javafx.scene.control.Tab;
import org.apache.taglibs.standard.tag.common.core.ForEachSupport;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 餐桌控制器
 * @author zy
 * @Version
 * @date 2021/01/26
 */
@WebServlet("/table")
public class TableController extends BaseServlet {

    private TableService tableService = new TableServiceImpl();

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryAll(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<DinnerTable> tables = tableService.queryAll();

        request.setAttribute("tables",tables);

        return ServletConstant.PRE_FORWARD + "/backend/detail/table/table-list.jsp";
    }

    /**
     * 删除
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String del(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数
        String dtIdStr = request.getParameter("dtId");
        //将字符串类型转换为整型
        int dtId = Integer.parseInt(dtIdStr);
        //根据餐桌 id模糊查询餐桌的信息
        tableService.deleteByTableId(dtId);
        //重定向到查询接口
        return ServletConstant.PRE_REDIRECT + "/table?method=search";
    }

    /**
     * 回显
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryByTableId(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //获取请求参数
        String dtIdStr = request.getParameter("dtId");
        //将字符串类型转换为整型
        int dtId = Integer.parseInt(dtIdStr);
        //根据菜类别名id查询菜类别的信息
        DinnerTable table = tableService.queryByTableId(dtId);
        //将查询到的信息放到 request域中
        request.setAttribute("table",table);
        //转发
        return ServletConstant.PRE_FORWARD + "/backend/detail/table/table-list.jsp";
    }

    /**
     * 添加
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //获取请求参数，餐桌名
        String dtName = request.getParameter("dtName");
        DinnerTable dinnerTable = new DinnerTable();
        dinnerTable.setDtName(dtName);
        dinnerTable.setDtState("0");
        //根据菜品类别名增加菜品类别
        tableService.saveTable(dinnerTable);
        //重定向到查询接口
        return ServletConstant.PRE_REDIRECT + "/table?method=search";
    }

    /**
     * 检验添加的餐桌名是否存在的方法
     * @param request
     * @param response
     */
    public String checkTableName(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String dtName = request.getParameter("dtName");
        int result = tableService.checkTableName(dtName);
        return String.valueOf(result);
    }

    /**
     * 搜索
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String search(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数
        String dtName = request.getParameter("dtName");

        List<DinnerTable> types = tableService.queryTableName(dtName);
        //将查询到的信息放入request域中
        request.setAttribute("types",types);
        request.setAttribute("dtName",dtName);
        //转发
        return ServletConstant.PRE_FORWARD + "/backend/detail/table/table-list.jsp";
    }

    /**
     * 更新餐桌状态
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数，餐桌id和餐桌状态
        String dtIdStr = request.getParameter("dtId");
        String status = request.getParameter("state");

        int dtId = Integer.parseInt(dtIdStr);

        DinnerTable table = new DinnerTable();

        table.setDtId(dtId);
        //判断桌子状态
        if (status.equals("0")){
            table.setDtState("1");
            table.setDtPresetTime(new Date());
        }else {
            table.setDtState("0");
            table.setDtPresetTime(null);
        }
        //更新餐桌信息
        tableService.update(table);

        return ServletConstant.PRE_REDIRECT + "/table?method=search";
    }
}
